package com.yhyr.mybatis.dynamic;

import com.alibaba.druid.pool.DruidDataSource;
import com.yhyr.mybatis.dynamic.datasource.DataSourceContextHolder;
import com.yhyr.mybatis.dynamic.datasource.DynamicDataSource;
import com.yhyr.mybatis.dynamic.domain.DbInfo;
import com.yhyr.mybatis.dynamic.domain.UserInfo;
import com.yhyr.mybatis.dynamic.services.DBService;
import com.yhyr.mybatis.dynamic.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.List;
import java.util.Map;

/**
 * Created by YHYR on 2017-12-25
 */

@SpringBootApplication
public class DynamicApplication implements CommandLineRunner{
	@Autowired
	UserService userService;

	public static void main(String[] args) {
		SpringApplication.run(DynamicApplication.class, args);
	}

	@Override
	public void run(String... strings) throws Exception {
		/**
		 * 切换为动态数据源实例，打印学生信息
		 */
		DataSourceContextHolder.setDBType("slave");
		List<UserInfo> list = userService.getUserInfo();
		list.stream().forEach(i -> System.out.println(i.toString()));

		DataSourceContextHolder.setDBType("default");
		List<UserInfo> list2 = userService.getUserInfo();
		list2.stream().forEach(i -> System.out.println(i.toString()));


	}
}
